<?php

namespace App\Services;

use App\Models\SchoolUser;
use App\Models\User;
use EasyWeChat\Kernel\Support\Arr;

class UserService
{
    // 创建用户
    public static function createUser($phone, $password, $nickname = '')
    {
        $user = new User;
        $user->nickname = $nickname == '' ? $phone : $nickname;
        $user->passport = $phone;
        $user->password = $password;
        $user->avatar = "http://pt-invoice-test2.oss-cn-shanghai.aliyuncs.com/images/%E5%87%AF%E6%80%9D.png";
        $user->save();
        $user_id = $user->id;
        return $user_id;
    }

    // 判断账号是否存在 并返回user_id
    public static function checkPassport($passport)
    {
        $user = User::where("passport", $passport)->first();
        $user_id = $user ? $user->id : 0;
        return $user_id;
    }

    // 检查老师是否存在于学校 或者校区
    public static function checkSchoolUser($schoolId, $campus_id, $phone)
    {
        return SchoolUser::where(
            [
                "school_id"=>$schoolId,
                "campus_id"=>[$campus_id, 0],
                "phone"=> $phone
            ]
         )->exists();
    }
}
